# coding=utf8

import urllib2
import urllib
import cookielib
from bs4 import BeautifulSoup

fp = open('bjh/bjh.txt', 'r')
bjh_list = []
while 1:
    line = fp.readline()
    bjh_list.append(line.strip())
    if not line:
        break
    pass
bjh_list.pop()
fp.close()

login_url = '***'
post_url = '***'


cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
reqReturn = urllib2.urlopen(login_url)


username = '1209050122'
password = 'bfyplsjq'

postdata = {
	'zjh':username,
	'mm':password,
	}

headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
           'Accept-Encoding':'gzip,deflate,sdch',
           'Accept-Language':'zh-CN,zh;q=0.8',
           'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36',
           'Content-Type':'application/x-www-form-urlencoded',
           'Referer':'http://xk1.swpu.edu.cn:9099/loginAction.do'}


postData = urllib.urlencode(postdata)
request = urllib2.Request(post_url, postData, headers)
response = urllib2.urlopen(request)

for l in bjh_list:
    URL = '***=' + l
    response = urllib2.urlopen(URL).read()
    result = response.decode('gbk').encode('utf8')
    soup = BeautifulSoup(result)
    jihe = soup.find_all(attrs = {"valign":"top", "width":"12%"})

    fp = open('db/' + l + '.txt', 'a')

    i = 1
    for e in jihe:
        if (e.get_text().strip()):
            if (e.get_text().strip().find(' ', 0) == -1):
                kecheng = str(i) + e.get_text().strip() + '\n'
                print kecheng
                fp.write(kecheng.encode('utf8'))
            else:
                kecheng = e.get_text().replace('    ', str(i)) + '\n'
                print kecheng
                fp.write(kecheng.encode('utf8'))
        i = i+1
    fp.close()